package com.gz.service;

import com.gz.common.Message;
import com.gz.common.MessageType;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

public class MessageClientService {
    //私聊
    public  void sendMessagetoOne(String content,String sender,String receiver){
        Message message = new Message();
        message.setContent(content);
        message.setSender(sender);
        message.setReceiver(receiver);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatedDate = simpleDateFormat.format(new Date());
        message.setSendTime(formatedDate);
        message.setMestype(MessageType.MESSAGE_COMM_MES);

        try {
            ObjectOutputStream oos = new ObjectOutputStream(
                    MangeClientConnectServerThread.getClientConnectServerThread(
                            sender).getSocket().getOutputStream());
            oos.writeObject(message);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //群发
    public  void sendMessagetoAll(String content,String sender){
        Message message = new Message();
        message.setContent(content);
        message.setSender(sender);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatedDate = simpleDateFormat.format(new Date());
        message.setSendTime(formatedDate);
        message.setMestype(MessageType.MESSAGE_TO_ALL_MES);

        try {
            ObjectOutputStream oos = new ObjectOutputStream(
                    MangeClientConnectServerThread.getClientConnectServerThread(
                            sender).getSocket().getOutputStream());
            oos.writeObject(message);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

